Skip to content

spacemit: pipeline flash attention decode QK and PV kernels - #96

Merged
alex-spacemit merged 2 commits into
spacemit-com:mtmd-backendfrom
co-seven:pr/nomtp-fa-20261203
Dec 4, 2026
Merged

alex-spacemit merged 2 commits into
spacemit-com:mtmd-backendfrom
co-seven:pr/nomtp-fa-20261203

Conversation

@co-seven

@co-seven co-seven commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Problem and behavior

Performance optimization following #87. Decode repeatedly reads K/V for groups of query heads. Pipeline RVV QK work across four query heads, reuse V loads across four or eight probability rows, and refine the transient IME2 QK path for eligible grouped-query geometry.

This PR deliberately excludes persistent packed-K caching. It has no extra cache object, second persistent K copy, cache lookup, invalidation or lifecycle changes. The IME2 QK path already existed in the baseline; this PR extends its eligible cases and kernel handling.

Change and design

  • rvv_kernels.cpp: four-head QK pipeline, PV4/PV8 helpers, shape-based dispatch and integration with the existing split-KV/softmax path.
  • ime2_kernels.cpp: QK kernel handling for the supported group sizes and tiles, using transient packed operands.
  • ime_kernels.h: corresponding internal kernel declarations.

All changes are kernel-local. The existing fallback handles unsupported geometry; no model names or new environment switches are used.

Scope is limited to ggml/src/ggml-spacemit/. Impact classification: execution-path change. No changes to the retired CPU backend, model code, public API or test sources. Local validation fixtures are excluded. This independently based module commit preserves the target branch's common MTP graph/rollback paths; experimental noMTP-wide reversions are not submitted. No new external dependency is added.

Validation

The K-pack call is now enclosed by the same RISCV64_SPACEMIT_IME2 compile-time guard as its setup. Without IME2 the existing RVV transpose remains available. The update changes no numerical kernels or dispatch thresholds; PV8 is unchanged.

The original head failed an ordinary non-IME2 Debug llama-bench link with an undefined ime2::fa_pack_f16_k8 reference. With this guard, the same configuration and target link successfully. Reproduce with the RISC-V toolchain and dependencies below, CMAKE_BUILD_TYPE=Debug, LLAMA_BUILD_TOOLS=ON, SPACEMIT_RISCV_COMPILER_SUPPORT_IME1=TRUE, and SPACEMIT_RISCV_COMPILER_SUPPORT_VMADOT_S4=FALSE, SPACEMIT_RISCV_COMPILER_SUPPORT_VPACK=FALSE, SPACEMIT_RISCV_COMPILER_SUPPORT_VNPACK=FALSE; build llama-bench. These preset capability results exercise the IME1-only build branch. The normal IME2 Release backend also rebuilds successfully. No new target runtime or PV8 numerical result is claimed.

Base: mtmd-backend at 3f5ce5fd84f4dc6e7e621642cfef0143d49e7c24. Candidate: 5449366e231c55fa8b7f6dc3837f96001871f21c; source tree e3ce8dc13ff5c956c9a085cc29ca033beadf5373.

The exact submitted production source passed a RISC-V Release cross-build of ggml-spacemit using the SpacemiT v1.1.2 toolchain (GCC 14.3), cmake/riscv64-spacemit-linux-gnu-gcc.cmake, GGML_SPACEMIT=ON, GGML_RV_ZBA=ON, GGML_OPENMP=OFF, Spert 0.6.3 and SpacemiT ORT 2.0.6. Build target: cmake --build <build> --target ggml-spacemit --parallel 24. Backend DSO SHA-256: 60987eab94ac29a576865291fe4db3f0b8e6c825cca67f7feefe65e908ce7d21. Source hashes were checked before/after the build and against the committed files.

The historical gemma-fa-kernels arm used transient K packing without a persistent K cache. All 128 generated IDs matched in all 12 formal requests. The selected Gemma geometry exercised the added decode routes. Qwen3.5-2B's attention geometry did not select them, so no Qwen3.5-2B benefit is claimed.

Static inspection of the final diff confirms that it changes only the three kernel files listed above. No packed-K cache header, backend-context member, allocation or graph-lifecycle hook is submitted.

Check Status Evidence / scope
Exact-source RISC-V backend build PASS Configuration and DSO identity above
Non-IME2 Debug executable link PASS (current source) IME1 enabled, IME2 disabled; llama-bench links; RVV object has no undefined fa_pack_f16_k8 reference
Diff / scope / formatting checks PASS Backend-only; no test changes or new environment controls; git diff --check, UTF-8/LF and added-line whitespace
Model outputs PASS (historical) All 128 IDs match in 12 Gemma requests
Kernel-only model performance PASS (historical measurement) Decode +9.96%, without persistent K caching
Publication scope PASS Three kernel files; no persistent packed-K cache
Final-head board / additional platform coverage NOT RUN Historical target results are carried forward; no new final-head K3 or K1 run

Performance

Case Baseline decode tok/s Candidate decode tok/s Samples per arm Change
Gemma 4 E2B Q4_0, 10K, no persistent K cache 12.289 ± 0.061 13.513 ± 0.076 6 +9.96%

Prefill: 250.36 → 249.99 tok/s (-0.15%). Historical comparison: 71a69aeb → 1332efafe, run label gemma-fa-kernels. This is the kernel-only result; separate packed-K-cache experiments are not included in the claimed benefit.

Historical K3 A/B protocol: X100 2.2 GHz / A100 1.8 GHz, eight backend workers on cores 8–15; fixed 10,240 prompt token IDs and 128 generated IDs; MTP off, FA on, F16 KV. Server: -np 1 -c 12288 -b 2048 -ub 512 -t 8 --device SPACEMIT0 -ngl 99 -fa on -lm none -ctk f16 -ctv f16 --no-cache-prompt --cache-ram 0 --spec-type none. Nonstreaming /completion: n_predict=128, ignore_eos=true, temperature=0, samplers=["temperature"], seed=123, return_tokens=true, cache_prompt=false.

ABBA order, four fresh server processes, one 16-token warmup and three formal requests per process: six samples across two startups per arm. Only the backend DSO changed. Runtime identity, frequencies and interference monitoring passed the archived audit. Decode throughput is 127 / decode_seconds; values are mean ± sample standard deviation. Delta = 100 * (candidate / baseline - 1). Prefill uses the server's prompt timing.

Risks and limits

The measured benefit applies to eligible Gemma attention geometry. Broader head dimensions/group sizes and the final independent PR head have not received new board runs.

The historical module comparisons used the common noMTP experimental baseline on top of 3f5ce5fd; this PR instead preserves upstream common MTP behavior. Historical results are not claimed as new measurements of the final independent head. Raw build/test/benchmark archives are retained privately and are not publicly attached; the cases, results, artifact identities and limits above are the reviewable record here. Exact-input model replay is not provided by these summaries. Required final-head target evidence remains pending; this submission requests review, not a waiver or a claim of merge readiness. No maintainer exception has been granted. CI status is separate from the local results above.

AI assistance and contributor responsibility

Codex assisted with source preparation, static review, local build/check execution, historical evidence analysis and this PR description. The contributor discussed the module design and explicitly requested publication. No unrecorded personal review, maintainer approval or target execution is asserted. The contributor remains responsible for reviewing the submitted code and claims and addressing review feedback.

Copy link
Copy Markdown
Collaborator

Please put the ime2::fa_pack_f16_k8 call in rvv_kernels.cpp under the same IME2 build guard as the nearby setup. Without IME2, CMake omits the implementation and the call currently relies on the compiler removing the false branch. Please check a non-IME2 Debug link. The PV8 helper also has a full FP32 branch, but its only caller passes packed FP16 probabilities; I'd remove that branch unless there is a caller for it.

The Gemma result covers PV4. Please add an exact-head K3 check for PV8 with 16 Q heads / 2 KV heads, D=256 and F16 KV. With eight workers, actual K extents 511/512/513 and query widths 1/3/4 give useful controls for split-KV, tail fallback and the single-mask pass. Please confirm the selected route as well as parity against CPU.

@alex-spacemit
alex-spacemit merged commit f76db4d into spacemit-com:mtmd-backend Dec 4, 2026
13 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants